package com.api.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by PC on 2017/7/10.
 */
public class OrderUtils {

    final Logger log = LoggerFactory.getLogger(this.getClass());

    private static Map<String, String> ORDER_CACHE = new HashMap<String, String>();

    ArrayList<String> myList = new ArrayList<String>();

    public OrderUtils() {

    }

    public OrderUtils(String column, String type) {
        if ("desc".equals(type)) {
            this.myList.add(column + " desc");
        }
        if ("asc".equals(type)) {
            this.myList.add(column + " asc");
        }

        if (null == type) {
            this.myList.add(column);
        }

    }

//        public static String columnFormat(String column){
//            String columnFormatted = ORDER_CACHE[column]
//            if (columnFormatted==null){
//                columnFormatted = column.collect{it == it.toUpperCase() ? "_"+it :it.toUpperCase()}.join("")
//                if (column.contains("Name") || column.contains("name") || column.contains("title") || column.contains("Title"))
//                    columnFormatted = String.format("convert(%s using gbk)", columnFormatted);
//                ORDER_CACHE."${column}" = columnFormatted
//            }
//            return columnFormatted;
//        }

    public static OrderUtils desc(String column) {
        return new OrderUtils(column, "desc");
    }

    public static OrderUtils asc(String column) {
        return new OrderUtils(column, "asc");
    }

    public void pushDesc(String column) {
        myList.add(column + " desc");
    }

    public void pushAsc(String column) {
        myList.add(column + " asc");
    }


    public String toString() {
        if (myList.size() == 0) return null;
        return org.apache.shiro.util.StringUtils.join(myList.iterator(), ", ");
    }

}
